package cn.edu.ctbu.test.api;


import cn.edu.ctbu.test.core.PageUtils;
import cn.edu.ctbu.test.domain.Course;
import cn.edu.ctbu.test.domain.Score;
import cn.edu.ctbu.test.domain.Student;
import cn.edu.ctbu.test.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController // RestController只能返回数据
@NoRepositoryBean
@RequestMapping("/api/course")
public class CourseRestController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/list")
    public List<Course> getAll(){
        List<Course> courses=courseService.findAll();

        return  courses;
    }

    @GetMapping("/getbypageCourse")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "condition",defaultValue = "") String condition)   {

        //按id排倒序
        Sort sort= Sort.by(Sort.Direction.ASC,"id");

        Page<Course> coursePage;

        if(ObjectUtils.isEmpty(condition)){
            Pageable pageable = PageRequest.of(page,size,sort);
            coursePage= courseService.findAll(pageable);
        }else {
            Course course = new Course();
            course.setCourseName(condition);

            Pageable pageable = PageRequest.of(0, 10, sort);

            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("condition", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Course> example = Example.of(course, matcher);

            coursePage = courseService.findAll(example, pageable);
        }

        PageUtils pageUtils=new PageUtils(coursePage.getContent(),coursePage.getTotalElements());

        return pageUtils;

    }

    @GetMapping("/get/{id}")
    public Course get(@PathVariable Long id){

        Course course=courseService.getById(id);

        return  course;//把id返回到客户端
    }




    /**
     * 以下为增删改查
     * @param
     * @return
     */
    @GetMapping("/findByName")
    public List<Course> findByName(String name){
        List<Course> courses=courseService.findByName(name);

        return courses;
    }

    @PostMapping("/insert") //添加人员不用返回人员，返回成功或者失败就可以了
    public Course insert(Course course){
        Course course1=courseService.insert(course);

        return course1;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id){

        courseService.delete(id);
    }

    @PutMapping("/update")
    public Course update(Course course){

        //读取旧成绩数据
        Course oldcourse=courseService.getById(course.getCourseId());

        course=courseService.update(course);

        return course;
    }
    
    
}
